﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebSite_CodeMan.Sys_CodeMan_Manage
{
    public partial class Admin_Class_Update : System.Web.UI.Page
    {
        protected string strLeave = "一级分类";
        protected void Page_Load(object sender, EventArgs e)
        {
            BaseClass.BaseClass.CheckState();
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            string strCID = Common.GetMes.GetRequestQuery(Request, "CID");
            string strTID = Common.GetMes.GetRequestQuery(Request, "TID");
            string strType = Common.GetMes.GetRequestQuery(Request, "Type");
            if (!string.IsNullOrEmpty(strType))
            {
                HLeave.Value = (string.IsNullOrEmpty(strTID)) ? strCID : strTID;

                OleDbDataReader DataRead = BLL.NoteInfo.GetNoteClassName(strTID);
                if (DataRead.HasRows)
                {
                    strLeave = "";
                    while (DataRead.Read())
                    {
                        strLeave += DataRead["T_ClassName"].ToString() + "&nbsp;〉&nbsp;";
                    }
                }
                DataRead.Close();
                DataRead.Dispose();
            }
            else if (!string.IsNullOrEmpty(strCID))
            {
                OleDbDataReader DataRead = BLL.NoteInfo.GetNoteClassMes(strCID);
                if (DataRead.HasRows)
                {
                    DataRead.Read();
                    txtClassName.Value = DataRead["T_ClassName"].ToString();
                    ViewState["en"]= txtEnName.Value = DataRead["T_ClassEnName"].ToString();
                    txtKey.Value = DataRead["T_ClassKey"].ToString();
                    txtDescription.Value = DataRead["T_ClassDes"].ToString();
                    txtList.Value = DataRead["T_ListTemplete"].ToString();
                    txtNote.Value = DataRead["T_NoteTemplete"].ToString();
                    HLeave.Value = DataRead["T_ClassLeave"].ToString();
                    strLeave = "当前类别";
                }
                DataRead.Close();
                DataRead.Dispose();
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string strType = Common.GetMes.GetRequestQuery(Request, "Type");
            string strCID = (string.IsNullOrEmpty(strType))?Common.GetMes.GetRequestQuery(Request, "CID"):Common.GetMes.GetNowTimeString();
            string strTID = (string.IsNullOrEmpty(HLeave.Value)) ? strCID : ((string.IsNullOrEmpty(strType)) ? HLeave.Value : HLeave.Value + "," + strCID);
            if (BLL.NoteInfo.EditClassMes(strCID, Server.HtmlEncode(txtClassName.Value.Trim()),Server.HtmlEncode(txtEnName.Value.Trim()),Server.HtmlEncode(txtDescription.Value.Trim()),Server.HtmlEncode(txtKey.Value.Trim()),strTID,"",strType,txtList.Value,txtNote.Value))
            {
                if (ViewState["en"] == null)
                {
                    if (!string.IsNullOrEmpty(txtEnName.Value.Trim()))
                    {
                        Common.FileComm.FolderCreate(Server.MapPath("~/" + txtEnName.Value.Trim()));
                    }
                }
                else if (ViewState["en"].ToString()!=txtEnName.Value.Trim())
                {
                    if (!string.IsNullOrEmpty(txtEnName.Value))
                    {
                        Common.FileComm.FolderCreate(Server.MapPath("~/" + txtEnName.Value.Trim()));
                    }
                    Common.FileComm.DeleteFolder(Server.MapPath("~/" + ViewState["en"].ToString()));
                }
                Common.MessageBox.Show(Page, "信息保存成功...", "Admin_Class_Mes.aspx", "suess");
            }
            else
            {
                Common.MessageBox.Show(Page, "信息保存失败，请重试或查看程序日志...", "error");
            }
        }
    }
}
